package com.cc.sanjieduan.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.cc.sanjieduan.dto.R;
import com.cc.sanjieduan.pojo.Device;
import com.cc.sanjieduan.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/devices")
public class DeviceController {

    private DeviceService deviceService;

    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @PostMapping("/command/{id}/{command}")
    public R sendController(@PathVariable String id, @PathVariable String command) throws JsonProcessingException {
        //向指定的设备发送命令
        deviceService.sendControl(id, command);
        return R.setOK();
    }


    @PostMapping("/device")
    public R addDevice(@RequestBody Device device) throws ExecutionException {
        //向指定的设备发送命令
        deviceService.addDevice(device);
        return R.setOK();
    }

}
